import ftplib

# 连接到FTP服务器
ftp = FTP('ftp.example.com')  # 替换为你想要连接的实际FTP地址
ftp.login(user='your_username', passwd='your_password')  # 登录账号和密码

# 检查目录存在与否
def check_directory_exists(directory_path):
    try:
        ftp.cwd(directory_path)
        return True
    except ftplib.error_perm as e:
        if "No such file or directory" in str(e):  # 如果返回错误代码550，则目录不存在
            return False
        else:
            raise e  # 其他错误抛出

# 示例目录路径
directory_to_check = '/path/to/directory'

if check_directory_exists(directory_to_check):
    print(f"The directory '{directory_to_check}' exists.")
else:
    print(f"The directory '{directory_to_check}' does not exist.")

# 关闭连接
ftp.quit()

# 若要下载文件，可以使用类似的方法：
def download_file_from_ftp(remote_file_path, local_file_path):
    with open(local_file_path, 'wb') as local_file:
        ftp.retrbinary(f'RETR {remote_file_path}', local_file.write)

# 下载文件，假设已知远程文件路径 remote_file_path
download_file_from_ftp('remote_file.txt', 'local_file.txt'12)